﻿unit NewReport;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  dxRibbonForm,
  cxGridExportLink,
  FIBQuery, pFIBCacheQueries,
  Dialogs, dxSkinsCore, dxSkinMcSkin, dxSkinSeven, dxSkinsdxRibbonPainter,
  dxStatusBar, dxRibbonStatusBar, cxClasses, dxRibbon, cxControls,
  dxSkinsdxBarPainter, dxBar, cxStyles, dxSkinscxPCPainter, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxGridLevel, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, dxPSGlbl, dxPSUtl, dxPSEngn, dxPrnPg, dxBkgnd,
  dxWrap, dxPrnDev, dxPSCompsProvider, dxPSFillPatterns, dxPSEdgePatterns,
  dxPSCore, dxPScxCommon, dxPScxGrid6Lnk, MemTableDataEh, MemTableEh, JvDialogs,
  cxLookAndFeels, cxLookAndFeelPainters, FIBDatabase, pFIBDatabase,
  dxSkinOffice2010Black, dxSkinOffice2010Blue, dxSkinOffice2010Silver,
  dxPSPDFExportCore, dxPSPDFExport, cxDrawTextUtils, dxPSPrVwStd, dxPSPrVwAdv,
  dxPSPrVwRibbon, dxPScxEditorProducers, dxPScxExtEditorProducers,
  dxPScxPageControlProducer, dxBarSkinnedCustForm, cxContainer, Menus,
  dxLayoutcxEditAdapters, dxLayoutControl, cxSplitter, cxDropDownEdit,
  cxSpinEdit, StdCtrls, cxButtons, cxTextEdit, cxMaskEdit, cxLookupEdit,
  cxDBLookupEdit, cxDBLookupComboBox, pFIBScripter, cxPropertiesStore;

type
  TNewRepForm = class(Tdxcustomribbonform)
    RibbonTab: TdxRibbonTab;
    dxRibbon1: TdxRibbon;
    stbar1: TdxRibbonStatusBar;
    barmen1: TdxBarManager;
    Bar1: TdxBar;
    btn_load: TdxBarButton;
    v1: TcxGridDBTableView;
    l1: TcxGridLevel;
    grd1: TcxGrid;
    rep1: TdxComponentPrinter;
    link: TdxGridReportLink;
    tbl: TMemTableEh;
    dlg: TJvOpenDialog;
    btn1: TdxBarButton;
    clAccCode: TcxGridDBColumn;
    clREESTR: TcxGridDBColumn;
    clYM: TcxGridDBColumn;
    clSUMMA: TcxGridDBColumn;
    clDSUMMA: TcxGridDBColumn;
    ds1: TDataSource;
    trn1: TpFIBTransaction;
    tbl_saldo: TMemTableEh;
    btn_update: TdxBarButton;
    db_temp: TpFIBDatabase;
    trn_temp: TpFIBTransaction;
    v2: TcxGridDBTableView;
    clAcCcode1: TcxGridDBColumn;
    clusr1: TcxGridDBColumn;
    cllasttime1: TcxGridDBColumn;
    clAcCcode2: TcxGridDBColumn;
    clusr2: TcxGridDBColumn;
    cllasttime2: TcxGridDBColumn;
    clName1: TcxGridDBColumn;
    clName2: TcxGridDBColumn;
    t_temp: TMemTableEh;
    ds_temp: TDataSource;
    clkontr1: TcxGridDBColumn;
    clkontr2: TcxGridDBColumn;
    lcontr1: TdxLayoutControl;
    cxButton1: TcxButton;
    intedt1: TcxSpinEdit;
    cbb2: TcxComboBox;
    lgrp1: TdxLayoutGroup;
    lgrpGrp: TdxLayoutGroup;
    lgrpGrplcontr1Group1: TdxLayoutGroup;
    lItemlcontr1Item3: TdxLayoutItem;
    lItemlcontr1Item2: TdxLayoutItem;
    sep: TdxLayoutSeparatorItem;
    lItem1: TdxLayoutItem;
    cxspltr1: TcxSplitter;
    script1: TpFIBScripter;
    prop1: TcxPropertiesStore;
    trn2: TpFIBTransaction;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btn_loadClick(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormDeactivate(Sender: TObject);
    procedure btn_updateClick(Sender: TObject);
    procedure v2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    { Private declarations }
    YM: string;
    function ClearStr(s :string) :string;
    function GetReestr(dt1,dt2 :TDate) :Integer;
    procedure CreateTable;
    procedure LoadData(fName,YM :string; reestr :Integer);
    procedure InsertData(REESTR: integer; YM :string);

    procedure CalcSaldo;
  public
    { Public declarations }
  end;

var
  NewRepForm: TNewRepForm;

implementation

uses cmUtils, DModule, MainFrm, NewRepParamFrm;

{$R *.dfm}

procedure TNewRepForm.btn1Click(Sender: TObject);
var
  q :TFIBQuery;
begin
   if Application.MessageBox(PChar('Выдействительно хотите выполнить расчет оплат УТСК?'),
      PChar('Внимание!'), MB_OKCANCEL + MB_ICONWARNING + MB_DEFBUTTON2) = IDOK
   then
   begin
     CalcSaldo;
    //q.ExecQuery;  //генерирую ошибку
   end;
end;

{------------------------------------------------------------------------------
 CalcSaldo:  Пересчитываем сальдо
_______________________________________________________________________________}
procedure TNewRepForm.CalcSaldo;
var
  Q,QOpl :TFIBQuery;
  accode :string;
  Summa,RSumma,OplSum,SchetSum,OstSum :Currency;
  ls :TStringList;
  i :Integer;
  d1,d2 :TDate;
  procedure AddScript;
  begin
    tbl_saldo.First;
    while not tbl_saldo.Eof do
    begin
      if tbl_saldo.FieldByName('OplSum').AsCurrency>0 then
         {ls.Add('UPDATE n_reestrstat set'+
                ' Summa='+CurrToStr(tbl_saldo.FieldByName('OplSum').AsCurrency)+','+
                ' DSumma='+CurrToStr(tbl_saldo.FieldByName('DSUMMA').AsCurrency)+','+
                ' YM='#39+YM+#39+
                ' where ACCCODE='+#39+tbl_saldo.FieldByName('ACCCODE').AsString+#39+
                ' and REESTR='+IntTOstr(tbl_saldo.FieldByName('REESTR').AsInteger)+';');}
      ls.Add('insert into N_REESTRSTAT (REESTR, YM, SUMMA, DSUMMA, ACCCODE)'+
            ' values ('+IntTOstr(tbl_saldo.FieldByName('REESTR').AsInteger)+
            ','#39+YM+#39','+CurrToStr(tbl_saldo.FieldByName('OplSum').AsCurrency)+
            ','+CurrToStr(tbl_saldo.FieldByName('DSUMMA').AsCurrency)+
            ','#39+tbl_saldo.FieldByName('ACCCODE').AsString+#39');');
       tbl_saldo.Next;
    end;
  end;
  //выполняем скрипт
  procedure ExecScript;
  var
    Q :TFIBQuery;
  begin
    if ls.Count>0 then
    begin
      if not trn2.Active=true then trn2.StartTransaction;
      Q:=GetQueryForUse(trn2,'DELETE FROM N_REESTRSTAT WHERE YM='#39+YM+#39);
      Q.ExecQuery;
      trn2.Commit;

      script1.Script:=ls;
      script1.AutoDDL:=False;
     // script1.Transaction:=trn2;
      try
        script1.ExecuteScript;
        if script1.Transaction.InTransaction then
                        script1.Transaction.Commit;
        //stbar1.Panels[0].Text:='Формирование завершенно.';
      finally
        if not trn2.Active=true then trn2.StartTransaction;
      end;
    end;
  end;

  procedure MakeList(Sum :Currency);
  begin
    tbl_saldo.First;
    while not tbl_saldo.Eof do
    begin
     SchetSum:=tbl_saldo.FieldByName('Summa').AsCurrency -
               tbl_saldo.FieldByName('RSumma').AsCurrency;

     if (SchetSum>=Sum) and (Sum>0) then
       begin
         tbl_saldo.Edit;
         tbl_saldo.FieldByName('OplSum').AsCurrency:=Sum;
         tbl_saldo.Post;
         Sum:=0;
       end else
       begin
         if Sum>0 then
         begin
           tbl_saldo.Edit;
           tbl_saldo.FieldByName('OplSum').AsCurrency:=SchetSum;
           tbl_saldo.Post;
           Sum:=Sum-Schetsum;
         end;
       end;
     tbl_saldo.Next;
    end;
    if Sum>0 then
    begin
      tbl_saldo.Last; //Vlad <- tyt
      tbl_saldo.Edit;
      tbl_saldo.FieldByName('DSUMMA').AsCurrency:=Sum;
      tbl_saldo.Post;
    end;
    AddScript;
  end;

begin
  ym:=GetYM(cbb2.ItemIndex+1,intedt1.Value);
  d1:=GetPeriodYm(YM,false);
  d2:=GetPeriodYm(YM,true);
  ls:=TStringList.Create;
  //получаем незакрытые счета
  if not trn1.Active=true then trn1.StartTransaction;

  Q:=GetQueryForUse(trn1,
  'select r.code,r.acCode,r.Summa, r.RSumma,r.reestr,'+
  ' iif(r.kontragent in ('#39'1-17'#39','#39'1-1'#39'),'+
   #39';4000115;4000113;'#39','#39';2548230148;2548137475;'#39') SRV'+
  ' from  n_reestrdata r'+
  //' left outer JOIN spr$schet ss on ss.code=r.accode'+
  ' where r.Summa>r.RSumma order by r.AcCode, r.Code');
  Q.ExecQuery;
  Accode:='';
   //получаем сумму оплат
   Qopl:=GetQueryForUse(trn1,
   'select Sum(Summa)*-1 OplSum from SchetPrv a'+
   ' where Val_Date BETWEEN :d1 and :d2'+
   ' and AccCode=:AcCode and Prv_Type=1'+
   ' and (pos('#39';'#39' || a.Srv || '#39';'#39', :srvlist)<>0)');
   i:=0;
  while not Q.Eof do
  begin

    //Summa:=Q.FN('Summa').AsCurrency;        //сумма переданного долга
    //RSumma:=Q.FN('RSumma').AsCurrency;      //сумма оплат по реестру

    if accode=Q.FN('Accode').AsString then  //повторный счет
    begin
     //добавляем счета во временную таблицу
       tbl_saldo.Append;
        tbl_saldo.FieldByName('ACCCODE').AsString:=Q.FN('acCode').AsString;
        tbl_saldo.FieldByName('REESTR').AsInteger:=Q.FN('reestr').AsInteger;
        tbl_saldo.FieldByName('SUMMA').AsCurrency:=Q.FN('Summa').AsCurrency;
        tbl_saldo.FieldByName('RSUMMA').AsCurrency:=Q.FN('RSumma').AsCurrency;
        tbl_saldo.FieldByName('DSUMMA').AsCurrency:=0;
        //tbl_saldo.FieldByName('OplSum').AsCurrency:=OplSum;
       tbl_saldo.Post;
    end else
    begin                                   //первый счет

     QOpl.ParamByName('AcCode').AsString:=Q.FN('acCode').AsString;
     QOpl.ParamByName('d1').AsDate:=d1;
     QOpl.ParamByName('d2').AsDate:=d2;
     QOpl.ParamByName('srvlist').AsString:=Trim(Q.FN('srv').AsString);
     QOpl.Prepare;
     QOpl.ExecQuery;

     OplSum:=StrToCurrDef(QOpl.FN('OplSum').AsString,0);  // сумма оплат за период
          //проверить временную таблицу на наличие записей
     //в случае если в ней есть записи уходим на обработку
     if not tbl_saldo.IsEmpty then
     begin
       //обрабатываем записи, добавляем в скрипт UPDATE
       MakeList(OplSum);
       tbl_saldo.EmptyTable;
     end;
     if OplSum>0 then                         //если сумма оплат больше 0
     begin
       //SchetSum:=Summa-RSumma;                // остаток долга
       tbl_saldo.Append;
        tbl_saldo.FieldByName('ACCCODE').AsString:=Q.FN('acCode').AsString;
        tbl_saldo.FieldByName('REESTR').AsInteger:=Q.FN('reestr').AsInteger;
        tbl_saldo.FieldByName('SUMMA').AsCurrency:=Q.FN('Summa').AsCurrency;
        tbl_saldo.FieldByName('RSUMMA').AsCurrency:=Q.FN('RSumma').AsCurrency;
        tbl_saldo.FieldByName('DSUMMA').AsCurrency:=0;
       tbl_saldo.Post;
       {if OplSum>=SchetSum then               // если оплаты больше переданного долга
       begin
         OplSum:=OplSum-SchetSum;             // остаток оплат
         ls.Add('UPDATE n_reestrstat set'+
                ' Summa='+CurrToStr(SchetSum)+','+
                ' DSumma='+CurrToStr(0)+
                ' where ACCCODE='+#39+accode+#39+
                ' and REESTR='+Q.FN('reestr').AsString+';');
       end else
       begin                                  // если оплаты меньше переданного долга

       end;}
     end;
     accode:=Q.FN('Accode').AsString;
    end;
    inc(i);
    stbar1.Panels[0].Text:=IntToStr(i);
    Application.ProcessMessages;
    Q.Next;
  end;
  ls.SaveToFile(ExtractFilePath(Application.ExeName)+'ls.sql');
  ExecScript;
  FreeQueryForUse(Q);
  FreeQueryForUse(QOpl);
  ls.Free;
  ShowMessage('ok');
end;

{-------------------------------------------------------------------------------
InsertData | Вставляем данные в таблицу
-------------------------------------------------------------------------------}
procedure TNewRepForm.InsertData(REESTR: integer; YM :string);
var
  sql :string;
  Q :TFIBQuery;
begin
 if trn1.Active=False then trn1.StartTransaction;
 if tbl.IsEmpty then Exit;
   sql:=
    'insert into N_REESTRSTAT (REESTR, YM, SUMMA, DSUMMA, ACCCODE)'+
    ' values (:REESTR, :YM, :SUMMA, :DSUMMA, :ACCCODE);';
   Q:=GetQueryForUse(trn1,sql);
 while not tbl.Eof do
 begin
   Q.ParamByName('REESTR').AsInteger:=tbl.FieldByName('REESTR').AsInteger;
   Q.ParamByName('YM').AsString:=YM;
   Q.ParamByName('ACCCODE').AsString:=tbl.FieldByName('ACCCODE').AsString;
   Q.ParamByName('SUMMA').AsCurrency:=tbl.FieldByName('SUMMA').AsFloat;
   Q.ParamByName('DSUMMA').AsCurrency:=tbl.FieldByName('DSUMMA').AsFloat;
   Q.ExecQuery;
   stbar1.Panels[0].Text:=IntToStr(tbl.RecNo);
   Application.ProcessMessages;
   tbl.Next;
 end;
  trn1.Commit;
  FreeQueryForUse(Q);
  stbar1.Panels[1].Text:='Load '+IntTostr(REESTR)+'-'+YM;
  //ShowMessage('Данные загружены');
end;
{-------------------------------------------------------------------------------
GetReestr | Получаем реестр по дате
-------------------------------------------------------------------------------}
function TNewRepForm.GetReestr(dt1,dt2 :TDate) :Integer;
var
  Q :TFIBQuery;
begin
  Result:=0;
  Q:=GetQueryForUse(dm1.trnRD,
  'Select Code from n_Reestr where d1>=:d1 and d1<=:d2 and kontragent=:kt');
  Q.ParamByName('d1').AsDate:=dt1;
  Q.ParamByName('d2').AsDate:=dt2;
  Q.ParamByName('kt').AsString:='1-17';
  Q.ExecQuery;
  if Q.RecordCount>0 then
  Result:=Q.FieldByName('Code').AsInteger;
  FreeQueryForUse(Q);
end;

procedure TNewRepForm.btn_loadClick(Sender: TObject);
var
  form :TNewRepParamForm;
  reestr,i :Integer;
  ym,fname,txtname :string;
  ls :TStringList;
begin
 ls:=TStringList.Create;
 FindFiles('c:\Projects\Rezident\Диспетчер\УТСК отчеты за год\all\','*.txt',
            ls,False);
 for I := 0 to Ls.Count - 1 do
 begin
   fname:= ls[i];
   txtname:=Copy(ExtractFileName(fname),1,Length(ExtractFileName(fname))-4);
   reestr:=StrToInt(GetWord(txtname,2,'_'));
   ym:=GetWord(txtname,1,'_');
   if not tbl.IsEmpty then tbl.EmptyTable;
   LoadData(fname,ym,reestr);
 end;
   stbar1.Panels[1].Text:='Данные загружены';
 {if dlg.Execute then
 begin
   //
     fname:= dlg.FileName;
     txtname:=Copy(ExtractFileName(fname),1,Length(ExtractFileName(fname))-4);
     reestr:=StrToInt(GetWord(txtname,2,'_'));
     ym:=GetWord(txtname,1,'_');
   form:=TNewRepParamForm.Create(nil);
   form.edt_ym.Text:=ym;
   form.edt_reestr.Text:=IntToStr(reestr);
   form.ShowModal;
   if form.ModalResult=mrOk then
   begin
     if not tbl.IsEmpty then tbl.EmptyTable;
     //reestr:=GetReestr(form.dt1.Date,form.dt2.Date);
     //reestr:=StrToInt(form.edt_reestr.Text);

     if reestr>0 then
     begin
       //ym:=FormatDateTime('YY',form.dt1.Date)
       //  +FormatDateTime('MM', form.dt1.Date);
       LoadData(fname,ym,reestr);
     end;
   end;
 end;}
 ls.Free;
end;
//временная - удалить
procedure TNewRepForm.btn_updateClick(Sender: TObject);
var
  Q,Q1 :TFIBQuery;
  ls :TStringList;
begin
 with t_temp.FieldDefs do
 begin
   Add('AcCcode1',ftString,32);
   Add('usr1',ftInteger);
   Add('Name1',ftString,255);
   Add('lasttime1',ftDateTime);
   Add('kontr1',ftString,255);

   Add('AcCcode',ftString,32);
   Add('usr',ftInteger);
   Add('Name',ftString,255);
   Add('lasttime',ftDateTime);
   Add('kontr',ftString,255);
 end;
  t_temp.CreateDataSet;
  t_temp.Open;
 db_temp.SQLDialect:=3;
 db_temp.DatabaseName:='localhost:c:\projects\base\RK.FDB';
 db_temp.ConnectParams.UserName:='sysdba';
 db_temp.ConnectParams.Password:='masterkey';
 db_temp.Connected:=True;
 trn_temp.StartTransaction;
 ls:=TStringList.Create;
 Q:=GetQueryForUse(trn1,
    'select r.AcCcode ,r.usr,su.name,r.lasttime,sk.name kontr from reestr r'+
    ' left outer join spr$users su on su.code=r.usr'+
    ' LEFT outer join spr$kontragent sk on sk.code=r.kontragent'+
    ' WHERE r.lasttime>'#39'22.09.10'#39);
 if not trn1.Active=true then trn1.StartTransaction;
 v2.DataController.DataSource:=nil;
 Q.ExecQuery;
 Q1:=GetQueryForUse(trn_temp,
      'select r.AcCcode AcCcode1,r.usr usr1,su.name Name1,r.lasttime lasttime1'+
      ' ,sk.name kontr1'+
      ' from reestr r'+
      ' left outer join spr$users su on su.code=r.usr'+
      ' LEFT outer join spr$kontragent sk on sk.code=r.kontragent'+
      ' WHERE r.AcCcode=:AcCcode');
 while not Q.Eof do
 begin
  Q1.ParamByName('AcCcode').AsString:=Q.FN('AccCode').AsString;
  Q1.ExecQuery;
  if Q.FN('usr').AsInteger<>Q1.FN('usr1').AsInteger then
  begin
  t_temp.Append;
   t_temp.FieldByName('AcCcode1').AsString:=Q1.FN('AcCcode1').AsString;
   t_temp.FieldByName('usr1').AsInteger:=Q1.FN('usr1').AsInteger;
   t_temp.FieldByName('Name1').AsString:=Q1.FN('Name1').AsString;
   t_temp.FieldByName('lasttime1').AsDateTime:=Q1.FN('lasttime1').AsDateTime;
   t_temp.FieldByName('kontr1').AsString:=Q1.FN('kontr1').AsString;

   t_temp.FieldByName('AcCcode').AsString:=Q.FN('AcCcode').AsString;
   t_temp.FieldByName('usr').AsInteger:=Q.FN('usr').AsInteger;
   t_temp.FieldByName('Name').AsString:=Q.FN('Name').AsString;
   t_temp.FieldByName('lasttime').AsDateTime:=Q.FN('lasttime').AsDateTime;
   t_temp.FieldByName('kontr').AsString:=Q.FN('kontr').AsString;
  t_temp.Post;
   // ls.Add('UPDATE N_REESTRDATA SET USR = '+Q1.FN('usr').AsString+
   // ', WHERE (AcCODE = '#39+Q.FN('AccCode').AsString+#39+');');
  end;

  Q.Next;
  stbar1.Panels[0].Text:=IntToStr(ls.Count);
  Application.ProcessMessages;
 end;
 v2.DataController.DataSource:=ds_temp;
 ls.SaveToFile(ExtractFilePath(Application.ExeName)+'UpdateUsr.sql');
 ls.Free;
end;
procedure TNewRepForm.v2CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  ExportGridToExcel(ExtractFilePath(Application.ExeName)+
              'UpdateUsr.xls',grd1,true,True,True,'xls');
end;
{-------------------------------------------------------------------------------
ClearStr | Очищаем от пробелов
-------------------------------------------------------------------------------}
function TNewRepForm.ClearStr(s :string) :string;
var
  i :Integer;
begin
  for i := 1 to Length(s) -1 do
  begin
    if s[i]=#32 then
      Delete(s,i,1);
  end;
  Result:=s;
end;
{-------------------------------------------------------------------------------
LoadData | Загружаем данные
-------------------------------------------------------------------------------}
procedure TNewRepForm.LoadData(fName,YM :string; reestr :Integer);
var
  ls :TStringList;
  i :Integer;
  summa, dsumma,accode :string;
begin
  v1.DataController.DataSource:=nil;
  ls:=TStringList.Create;
   ls.LoadFromFile(fName);
   for I := 0 to ls.Count - 1 do
   begin
     accode:=ClearStr(GetWord(ls[i],1,#9));
     summa:=ClearStr(GetWord(ls[i],2,#9));
     dsumma:=ClearStr(GetWord(ls[i],3,#9));
     tbl.Append;
     tbl.FieldByName('ACCCODE').AsString:=accode;
     tbl.FieldByName('REESTR').AsInteger:=reestr;
     tbl.FieldByName('YM').AsString:=YM;
     if summa='' then summa:='0';
     if dsumma='' then dsumma:='0';
     tbl.FieldByName('SUMMA').AsString:=summa;
     tbl.FieldByName('DSUMMA').AsString:=dsumma;
     tbl.Post;
   end;
  ls.Free;
  tbl.First;
   {if Application.MessageBox('Загрузить данные в базу?', 'Внимание!',
   MB_YESNO + MB_ICONQUESTION + MB_DEFBUTTON2 + MB_TOPMOST) = IDYES then
   begin }
    InsertData(reestr,ym);
   {end;}
   v1.DataController.DataSource:=ds1;
end;


{-------------------------------------------------------------------------------
CreateTable | Создаем таблицу в памяти
-------------------------------------------------------------------------------}
procedure TNewRepForm.CreateTable;
begin
 with tbl.FieldDefs do
 begin
   Add('ACCCODE',ftString,32,True);
   Add('REESTR',ftInteger);
   Add('YM',ftString,4);
   Add('SUMMA',ftFloat);
   Add('DSUMMA',ftFloat);
 end;
  tbl.CreateDataSet;
  tbl.Open;

  with tbl_saldo.FieldDefs do
  begin
   Add('ACCCODE',ftString,32,True);
   Add('REESTR',ftInteger);
   Add('SUMMA',ftFloat);
   Add('RSUMMA',ftFloat);
   Add('DSUMMA',ftFloat);
   Add('OplSum',ftFloat);
   Add('YM',ftString,4);
  end;
   tbl_saldo.CreateDataSet;
   tbl_saldo.Open;
end;

procedure TNewRepForm.FormActivate(Sender: TObject);
begin
 if UserCod='2' then
 MainForm.bar_newrep.Visible:=True;
end;

procedure TNewRepForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  MainForm.bar_newrep.Visible:=false;
  RemoveBtn(Name);
  Action:=caFree;
end;

procedure TNewRepForm.FormCreate(Sender: TObject);
begin
  AddButton(self);
  if UserCod='2' then
  MainForm.bar_newrep.Visible:=True;
  CreateTable;
   prop1.StorageName:=ExtractFilePath(Application.ExeName)+'config\NewRep';
   prop1.Active:=True;
   if FileExists(ExtractFilePath(Application.ExeName)+'config\NewRep') then
   prop1.RestoreFrom;
end;


procedure TNewRepForm.FormDeactivate(Sender: TObject);
begin
 MainForm.bar_newrep.Visible:=false;
end;

end.
